package com.example.bootmq;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 消费者消费消息.
 *
 * @author Xu Yayuan
 * @date 2022-09-03
 */
@Component
@RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
public class RabbitMQListener {

    @RabbitHandler
    public void testMsg(String body, Message message, Channel channel) throws IOException {
        long msgTag = message.getMessageProperties().getDeliveryTag();

        // 手工确认消费消息 true 开启多条，false 一条条的确认  告诉broker消息已经被消费者确认消费了消息
        channel.basicAck(msgTag, false);
        // 告诉broker消息拒收，
//        channel.basicNack(msgTag, false, true);
//        channel.basicReject(msgTag, false);
        System.out.println("msgTag :" + msgTag);
        System.out.println("msg :" + message);
        System.out.println("开始消费消息 body :" + body);
    }
}
